using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DocLibApp
{
    public partial class FormTableColumn : FormBase
    {
        public FormTableColumn()
        {
            InitializeComponent();

            PopulateTableList();
        }

        private void PopulateTableList()
        {
            System.Collections.ArrayList alist = FormMain.DocLib_SQLite.GetListofTables();
            foreach (object obj in alist)
            {
                string tablename = obj.ToString();
                cboTables.Items.Add(tablename);
            }
        }

        private void GetColumnsInfo(string tablename)
        {
            if (lsv_Columns.Items.Count > 0)
                lsv_Columns.Items.Clear();

            System.Collections.Hashtable ht = FormMain.DocLib_SQLite.GetTableColumnInfo(tablename);
            foreach (string key in ht.Keys)
            {
                string Description = doc_system.GetColumnDescription(tablename, key);

                ListViewItem aitem = new ListViewItem(Description);
                aitem.Tag = key;
                aitem.SubItems.Add(key);

                lsv_Columns.Items.Add(aitem);
            }
        }

        private void SaveCurrenTableDescription()
        {
            if (lsv_Columns.Items.Count > 0)
            {
                wait_start();

                foreach (ListViewItem aitem in lsv_Columns.Items)
                {
                    string desc = aitem.Text.Trim();
                    string field = aitem.Tag.ToString();

                    doc_system.SetColumnDescription(cboTables.Text.Trim(), field, desc);
                }

                wait_end();

                ShowMessage(cboTables.Text.Trim() + " table column descriptions updated.");
            }
        }

        private void cboTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            string tablename = cboTables.Text;
            GetColumnsInfo(tablename);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveCurrenTableDescription();
        }
    }
}